package kr.co.softtrain.glc.control;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class NavigationBar extends FrameLayout implements OnClickListener {
	
	private final int SIDE_PADDING = 10;
	private int m_nStyle = -1;
	
	public static enum controlPosition {
        LEFT, RIGHT  
    }
	
	public NavigationBar(Context context) {
		super(context);
		init(context);
	}
	
	public NavigationBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	private ImageView m_imgBg = null;
	private RelativeLayout m_rlMain = null;
	
	private final void init(Context context){
		
		// Full Frame Params
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.CENTER_VERTICAL;

		setLayoutParams(params);
		
		// Navigation Background Image
		m_imgBg = new ImageView(context);
		
		addView(m_imgBg);
		
		// Navigation Control
		m_rlMain = new RelativeLayout(context);
		m_rlMain.setLayoutParams(params);
	
		// Left & Right Layout Initialization
		initLayoutButtonLeft();
		initLayoutButtonRight();
		
		addView(m_rlMain);
	}
	
	public void setBackgoundImage(Bitmap bm) {
		
		if (m_imgBg != null && bm != null) {
			m_imgBg.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, bm.getHeight()));
			m_imgBg.setScaleType(ScaleType.FIT_XY);

			m_imgBg.setImageBitmap(bm);
		}
	}
	
	public static final int LEFT_BUTTON_LAYOUT = 0x2090;
	public static final int RIGHT_BUTTON_LAYOUT = 0x2091;
	
	private LinearLayout m_llLeft = null;
	private LinearLayout m_llRight = null;
	
	private void initLayoutButtonLeft() {
		
		if (m_rlMain != null) {
			RelativeLayout.LayoutParams paramsLeft = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			paramsLeft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			paramsLeft.addRule(RelativeLayout.CENTER_VERTICAL);
			
			m_llLeft = new LinearLayout(getContext());
			m_llLeft.setLayoutParams(paramsLeft);
			m_llLeft.setId(LEFT_BUTTON_LAYOUT);
			
			m_rlMain.addView(m_llLeft);
		}
	}
	
	private void initLayoutButtonRight() {
		
		if (m_rlMain != null) {
			RelativeLayout.LayoutParams paramsRight = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			paramsRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			paramsRight.addRule(RelativeLayout.CENTER_VERTICAL);
			
			m_llRight = new LinearLayout(getContext());
			m_llRight.setLayoutParams(paramsRight);
			m_llRight.setId(RIGHT_BUTTON_LAYOUT);
			
			m_rlMain.addView(m_llRight);
		}
	}
	
	// Title
	private ImageView m_imgBI = null;
	private TextView m_textTitle = null;
	
	public void setBI(int nRes) {
	
		if (m_rlMain != null) {
			
			initNavigationBarType();
		
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			params.addRule(RelativeLayout.CENTER_VERTICAL);
			params.leftMargin = 20;
			params.rightMargin = 20;
			
			if (m_nStyle != -1) {
				params.addRule(m_nStyle);
			}
			
			m_imgBI = new ImageView(getContext());
			m_imgBI.setImageResource(nRes);
			m_imgBI.setLayoutParams(params);
			
			m_rlMain.addView(m_imgBI);
			
			if (m_textTitle != null) {
				m_textTitle.setVisibility(View.GONE);
			}
		}
	}
	
	public void setTitle(String szTitle){
		if(m_textTitle != null){
			m_textTitle.setText(szTitle);
			return;
		}
		if (m_rlMain != null) {

			initNavigationBarType();
			
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			params.addRule(RelativeLayout.CENTER_VERTICAL);
			params.leftMargin = 20;
			params.rightMargin = 20;
			
			if (m_nStyle != -1) {
				params.addRule(m_nStyle);
			}
			
			m_textTitle = new TextView(getContext());
			m_textTitle.setTextColor(Color.parseColor("#413B39"));
			m_textTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16.5F);

			// �¿� �������� ���� ����
			m_textTitle.setSingleLine();
			m_textTitle.setSelected(true);
			m_textTitle.setEllipsize(TruncateAt.MARQUEE);

			m_textTitle.setLayoutParams(params);
			m_rlMain.addView(m_textTitle);
			m_textTitle.setText(szTitle);
			
			if (m_imgBI != null) {
				m_imgBI.setVisibility(View.GONE);
			}
		}
	}
	
	public void setSidePadding(boolean bLeft, boolean bRight){
		if(m_rlMain != null){
			int nLeft = 0;
			int nRight = 0;

			if(bLeft){
				nLeft = SIDE_PADDING; 
			}
			
			if(bRight){
				nRight = SIDE_PADDING;
			}
			
			m_rlMain.setPadding(nLeft, 0, nRight, 0);
		}
	}
	
	public void addChildControl(View view, controlPosition pos){
		
		if(view != null){
			if(pos == controlPosition.RIGHT){
				
				// ������ ��ư�� ���
				if (m_llRight != null) {
					m_llRight.addView(view);
				}
				
			} else if (pos == controlPosition.LEFT){
				
				// ���� ��ư�� ���
				if (m_llLeft != null) {
					m_llLeft.addView(view);	
				}
			}
			view.setOnClickListener(this);
		}
	}
	
	public void initNavigationBarType(){
		m_nStyle = RelativeLayout.CENTER_HORIZONTAL;
	}
	
	OnChildClickListener m_childClickListener = null; 

	public void setOnChildClickListener(OnChildClickListener listener) {
		m_childClickListener = listener;
	}
	
	public interface OnChildClickListener { 	
		public abstract void OnChildItemClick(View v); 
	}

	@Override
	public void onClick(View v) {
		if(m_childClickListener != null){
			m_childClickListener.OnChildItemClick(v);
		}
	}
}

